/*
 * template study 
 */

#include <iostream>

using namespace std;

/* 
 *  template function
 */
template <typename T>
T abs ( T x)
{
	return x < 0 ? -x : x;
}

int main()
{
	int n = -5;
	double d = 4.4;
	cout << abs(n) << endl;
	cout << abs(d) << endl;
}


/* ================================================================================  */

template < class T >
void outputArray(const T *p_array, const int count)
{
	for(int i = 0; i < count; i++)
		cout << p_array[i] << " " ;
	cout << end;
}

int main()
{
	const int acount = 8, bcount = 8, ccount = 20;
	int array[acount] = {1,2,3,4,5,6,7,8};
	double barray[bcount] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8};
	char carray[ccount] = "Welcome to see you !";

	cout << "a array contains: " << endl;
	outputArray(array, acount);

	cout << " b array contains: " << endl;
	outputArray(barray, bcount);

	cout << "c array contains: "  << endl;
	outputArray(carray, ccount);
}

/*
 * template class
 */
struct student
{
	int id;
	float gpa;
};

template < class T>
class store
{
	private:
		T item;
		int havevalue;
	public :
		store(void);

		T getelem(void);
		void putelem(T x);
};

template <class T>
store<T> :: store(void):
{
	
}

template < class T >
T store< T >::getelem(void)
{
	if(havevalue == 0) {
		cout << "no item present !" << endl;
		exit(1);
	}

	return item;
}

template < class T>
void store < T >::putelem( T x)
{
	havevalue++;
	item = x;
}

int main()
{
	student g = {1000, 23};
	store<int s1, s2;
	store<student> s3;

	store<double> D;
	s1.putelem(3);
	s2.putelem(-7);
	cout << s1.getelem() << " " << s2.getelem() << endl;
	s3.putelem(g);
	cout << "The student id w " < s3.getelem().id << endl;
	cout << "Retrieving object D";
	cout << D.getelem() << endl;
}

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 